const getBackCodeStr = (tableName,keyName,allFields) => {
    const fieldsName = allFields.filter(item=>item!==keyName)
    console.log(fieldsName)
 return     "import { connection } from '../util/mysql.mjs'; \n"+ 
            "import  querystring from 'querystring'; \n"+
            "const control = (req,res)=>{ \n"+
            "   const reg = /[?/]{1}/g; \n"+
            "   const urlArr = req.url.split(reg); \n"+
            "   if(urlArr.indexOf('query')!==-1){ \n" +
            "       queryMethod(req,res) \n" +
            "   }   \n"+    
            "   else if(urlArr.indexOf('add')!==-1){ \n"+
            "       insertMethod(req,res) \n" +
            "   }   \n"+  
            "   else if(urlArr.indexOf('update')!==-1){ \n"+
            "       updateMethod(req,res) \n" +
            "   }   \n"+
            "   else if(urlArr.indexOf('delete')!==-1){ \n"+
            "       deleteMethod(req,res) \n" +
            "   }   \n"+
            "} \n"+
            "const queryMethod = (req,res)=>{ \n"+
            "   const selectQuery = 'SELECT * FROM "+tableName.toUpperCase()+"'; \n"+
            "   connection.query(selectQuery, (err, results) => { \n"+
            "       if (err) { \n"+
            "       console.error('Error querying data:', err); \n"+
            "       return; \n"+
            "       }   \n"+
            "       else{ \n"+
            "       res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "       res.end(JSON.stringify(results)); \n"+
            "       }   \n"+
            "   }); \n"+
            "} \n"+
            "const insertMethod = (req,res)=>{ \n"+
            "   let parsedBody = null; \n"+
            "   if (req.method === 'POST') { \n"+
            "       let body = ''; \n"+
            "       req.on('data', (chunk) => { \n"+
            "           body += chunk.toString(); \n"+
            "       }); \n"+
            "       req.on('end', () => { \n"+
            "           parsedBody = querystring.parse(body); \n"+
            "           tryAdd(parsedBody) \n"+
            "       }); \n"+
            "   } \n"+
            "   function tryAdd(parsedBody){ \n"+
            "       let {"+fieldsName.join(',')+"} = parsedBody; \n"+
            "       if("+fieldsName.join('&&')+"){ \n"+
            "           const insertQuery = 'INSERT INTO "+tableName.toUpperCase()+" SET ? '; \n"+
            "           connection.query(insertQuery, {"+fieldsName.join(',')+"}, (err, result) => { \n"+
            "               if (err) { \n"+
            "               res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "               res.end(JSON.stringify({message:  'Error inserting data'})); \n"+
            "               }   \n"+
            "               else{ \n"+
            "               res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "               res.end(JSON.stringify({message:  '"+tableName+" added successfully'})); \n"+
            "               }   \n"+
            "           }); \n"+
            "       }   \n"+
            "       else{ \n"+
            "       res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "       res.end(JSON.stringify({message:  'Invalid data'})); \n"+
            "       }   \n"+
            "   } \n"+
            "} \n"+
            "const updateMethod = (req,res)=>{ \n"+
            "   let parsedBody = null; \n"+
            "   if (req.method === 'POST') { \n"+
            "       let body = ''; \n"+
            "       req.on('data', (chunk) => { \n"+
            "           body += chunk.toString(); \n"+
            "       }); \n"+
            "       req.on('end', () => { \n"+
            "           parsedBody = querystring.parse(body); \n"+
            "           tryUpdate(parsedBody) \n"+
            "       }); \n"+
            "   } \n"+
            "   function tryUpdate(parsedBody){ \n"+
            "       const {"+allFields.join(",")+"} = parsedBody; \n"+
            "       const updateQuery = 'UPDATE "+tableName.toUpperCase()+" SET ? WHERE "+keyName.toUpperCase()+" =?'; \n"+
            "       connection.query(updateQuery, [{"+allFields.join(",")+"},"+keyName.toLowerCase()+"], (err, result) => { \n"+
            "           if (err) { \n"+
            "           console.error('Error updating data:', err); \n"+
            "           res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "           res.end(JSON.stringify({message:  'Error update data'})); \n"+
            "           }   \n"+
            "           else{ \n"+
            "           res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "           res.end(JSON.stringify({message:  'success update data'})); \n"+
            "           }   \n"+
            "       }); \n"+
            "   } \n"+
            "} \n"+
            "const deleteMethod = (req,res)=>{ \n"+
            "   let parsedBody = null; \n"+
            "   if (req.method === 'POST') { \n"+
            "       let body = ''; \n"+
            "       req.on('data', (chunk) => { \n"+
            "           body += chunk.toString(); \n"+
            "       }); \n"+
            "       req.on('end', () => { \n"+
            "           parsedBody = querystring.parse(body); \n"+
            "           tryDelete(parsedBody) \n"+
            "       }); \n"+
            "   } \n"+
            "   function tryDelete(parsedBody){ \n"+
            "       let {"+keyName+"} = parsedBody; \n"+
            "       if("+keyName+"){ \n"+
            "           const deleteQuery = 'DELETE FROM "+tableName.toUpperCase()+" WHERE "+keyName+" =?'; \n"+
            "           connection.query(deleteQuery, ["+keyName+"], (err, result) => { \n"+
            "               if (err) { \n"+
            "               console.error('Error deleting data:', err); \n"+
            "               res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "               res.end(JSON.stringify({message:  '"+tableName+" delete err'})); \n"+
            "               }   \n"+
            "               else{ \n"+
            "               res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "               res.end(JSON.stringify({message:  '"+tableName+" delete successfully'})); \n"+
            "               }   \n"+
            "           }); \n"+
            "       }   \n"+
            "       else{ \n"+
            "       res.writeHead(200, { 'Content-Type': 'application/json' }); \n"+
            "       res.end(JSON.stringify({message:  'Invalid data'})); \n"+
            "       }   \n"+
            "   } \n"+
            "} \n"+
            "export  { control };";
}
export { getBackCodeStr };